package com.kxf.androidtestdemo.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.work.Operation;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager;

import com.google.common.util.concurrent.ListenableFuture;
import com.kxf.androidtestdemo.R;
import com.kxf.androidtestdemo.service.MyWorker;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class WorkManagerTestActivity extends BaseActivity {

    private Executor executor = Executors.newCachedThreadPool();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_work_manager_test);
    }

    public void startWm(View view) {
        Log.i(tag, "startWm===>>>");
        PeriodicWorkRequest request = new PeriodicWorkRequest.Builder(MyWorker.class, 15, TimeUnit.SECONDS).build();
        //任务入队，WorkManager调度执行
        Operation operation = WorkManager.getInstance().enqueue(request);
        final ListenableFuture<Operation.State.SUCCESS> re = operation.getResult();

        re.addListener(new Runnable() {
            @Override
            public void run() {
                try {
                    Log.i(tag, "addListener run==>>>" + re.get());
                } catch (ExecutionException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, executor);
    }
}